package models

import (
	"context"
	"go-chain-api/global"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type PoolTransaction struct {
	BlockNumber int64  `bson:"blockNumber" json:"blockNumber"`
	Status      uint64 `bson:"status" json:"status"`
	TxHash      string `bson:"txHash" json:"txHash"`
	Timestamp   uint64 `bson:"timestamp" json:"timestamp"`
	From        string `bson:"from" json:"from"`
	To          string `bson:"to" json:"to"`
	Amount0     string `bson:"amount0" json:"amount0"`
	Amount1     string `bson:"amount1" json:"amount1"`
	Type        int    `bson:"type" json:"type"` //1.加池 2.撤池
}

func GetPoolList(tokenSymbol string, pageIndex uint, pageSize uint, typeVal int) ([]PoolTransaction, error) {

	//根据时间戳倒序 查询所有数据
	var poolTransactions []PoolTransaction
	poolClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("USDT_" + tokenSymbol + "_pool_transaction")

	filter := bson.M{}

	if typeVal == 1 {
		filter["type"] = 1
	}

	if typeVal == 2 {
		filter["type"] = 2
	}

	optionFind := options.Find().SetSort(bson.D{{"timestamp", -1}}).SetLimit(int64(pageSize)).SetSkip(int64(pageIndex))

	find, err := poolClient.Find(context.Background(), filter, optionFind)

	if err != nil {
		return poolTransactions, err
	}

	err = find.All(context.Background(), &poolTransactions)

	return poolTransactions, err

}
